<?php

namespace App\Http\Controllers\Activity\Web;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Activity\ActivityValidate;
use App\Service\Activity\ActivityService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ActivityController extends BaseController
{
    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function index(Request $request): JsonResponse
    {
        $params = ActivityValidate::indexValidate($request->all());
        return $this->success(ActivityService::index($params));
    }

    /**
     * 新建活动
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception|GuzzleException
     */
    public function store(Request $request): JsonResponse
    {
        $params = ActivityValidate::storeValidate($request->all());
        ActivityService::store($request->user()->id, $params);
        return $this->success();
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function show(Request $request, int $id): JsonResponse
    {
        ActivityValidate::showValidate($request->merge(["id" => $id])->all());
        return $this->success(ActivityService::show($id));
    }


    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception|GuzzleException
     */
    public function update(Request $request,int $id): JsonResponse
    {
        $params = ActivityValidate::updateValidate($request->merge(["id" => $id])->all());
        ActivityService::update($request->user()->id, $params);
        return $this->success();
    }


    /**
     * 结束
     *
     * @param int $id
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function finish(int $id, Request $request): JsonResponse
    {
        ActivityValidate::finishValidate($request->merge(["id" => $id])->all());
        ActivityService::finish($request->user()->id, $id);
        return $this->success();
    }

    /**
     * 发布 | 暂停 | 启用
     *
     * @param int $id
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function status(int $id, Request $request): JsonResponse
    {
        $params = ActivityValidate::statusValidate($request->merge(["id" => $id])->all());
        ActivityService::status($request->user()->id, $params);
        return $this->success();
    }
}
